package com.crystal.jcms.frament;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.crystal.constant.Constant;
import com.crystal.jcms.shpz.ActivityFrame;
import com.crystal.jcms.shpz.ActivityMain;
import com.crystal.jcms.shpz.R;
import com.crystal.jcms.utils.RequestUtils;
import com.crystal.jcms.utils.ShowMsgUtils;
import com.crystal.jcms.utils.SpUtils;
import com.crystal.jcms.utils.RequestUtils.CallBack;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class MenuFrament extends BaseFragment {
	private static Context mContext;
	private GridView menuGridView;
	private ArrayList<Map<String, Object>> menuItemList;
	private ArrayList<Map<String, Object>> menuList;
	private SimpleAdapter menuSimpleAdapter;
	private String dbType;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = getActivity().getApplicationContext();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.content_menu, container, false);
		menuGridView = (GridView) view.findViewById(R.id.grid_menu);
		menuItemList = new ArrayList<Map<String, Object>>();
		menuList = new ArrayList<Map<String, Object>>();
		initMenu();
		menuSimpleAdapter = new SimpleAdapter(getActivity(), menuList, R.layout.gridview_item_menu,
				new String[] { "ItemImage", "ItemText" }, new int[] { R.id.ItemImage, R.id.ItemText });
		menuGridView.setOnItemClickListener(new MenuOnItemClick());
		getMenuData();
		return view;
	}

	// 初始化功能菜单
	private void initMenu() {
		Map<String, Object> item_shenhe = new HashMap<String, Object>();
		item_shenhe.put("ItemCode", "48882E32-6A7F-4C9D-9353-467D492EBA5E");
		item_shenhe.put("ItemImage", R.drawable.shenhe);
		item_shenhe.put("ItemText", "市站报告审核");
		menuItemList.add(item_shenhe);
		Map<String, Object> item_pizhun = new HashMap<String, Object>();
		item_pizhun.put("ItemCode", "99B70276-F240-41E7-BD53-535B5C6E1D4D");
		item_pizhun.put("ItemImage", R.drawable.pizhun);
		item_pizhun.put("ItemText", "市站报告批准");
		menuItemList.add(item_pizhun);
		Map<String, Object> item_jyshenhe = new HashMap<String, Object>();
		item_jyshenhe.put("ItemCode", "1B841C32-03F5-4112-B0A7-559995AA6ED6");
		item_jyshenhe.put("ItemImage", R.drawable.shenhe2);
		item_jyshenhe.put("ItemText", "建业报告审核");
		menuItemList.add(item_jyshenhe);
		Map<String, Object> item_jypizhun = new HashMap<String, Object>();
		item_jypizhun.put("ItemCode", "6160F307-741B-4012-A997-EC582B6002CC");
		item_jypizhun.put("ItemImage", R.drawable.pizhun2);
		item_jypizhun.put("ItemText", "建业报告批准");
		menuItemList.add(item_jypizhun);
		Map<String, Object> device_weixiushenpi = new HashMap<String, Object>();
		device_weixiushenpi.put("ItemCode", "5");
		device_weixiushenpi.put("ItemImage", R.drawable.shenhe);
		device_weixiushenpi.put("ItemText", "设备维修审批");
		menuItemList.add(device_weixiushenpi);
		Map<String, Object> device_weixiuqueren = new HashMap<String, Object>();
		device_weixiuqueren.put("ItemCode", "6");
		device_weixiuqueren.put("ItemImage", R.drawable.pizhun);
		device_weixiuqueren.put("ItemText", "设备维修确认");
		menuItemList.add(device_weixiuqueren);
		Map<String, Object> buhegereport_shenhe = new HashMap<String, Object>();
		buhegereport_shenhe.put("ItemCode", "7");
		buhegereport_shenhe.put("ItemImage", R.drawable.shenhe2);
		buhegereport_shenhe.put("ItemText", "不合格报告审核");
		menuItemList.add(buhegereport_shenhe);
		Map<String, Object> buhegereport_pizhun = new HashMap<String, Object>();
		buhegereport_pizhun.put("ItemCode", "8");
		buhegereport_pizhun.put("ItemImage", R.drawable.pizhun2);
		buhegereport_pizhun.put("ItemText", "不合格报告批准");
		menuItemList.add(buhegereport_pizhun);
		Map<String, Object> yangpin_shenhe = new HashMap<String, Object>();
		yangpin_shenhe.put("ItemCode", "9");
		yangpin_shenhe.put("ItemImage", R.drawable.shenhe2);
		yangpin_shenhe.put("ItemText", "样品处理审核");
		menuItemList.add(yangpin_shenhe);
		Map<String, Object> yangpin_pizhun = new HashMap<String, Object>();
		yangpin_pizhun.put("ItemCode", "10");
		yangpin_pizhun.put("ItemImage", R.drawable.pizhun2);
		yangpin_pizhun.put("ItemText", "样品处理批准");
		menuItemList.add(yangpin_pizhun);
	}

	private void getMenuData() {
		Map<String, String> params = new HashMap<String, String>();
		params.put("strUid", SpUtils.get(mContext, "Id", "").toString());
		RequestUtils.doPostAsyn(mContext, "menu.xml", "GetUserFunRightByUid", params, new CallBack() {

			@Override
			public void onRequestComplete(String result) {
				Message message = new Message();
				message.what = Constant.SHOWTOAST;
				if (result.contains("500_")) {
					message.obj = result.substring(4);
					handler.sendMessage(message);
					return;
				}
				try {
					JSONObject jsonResult = new JSONObject(result);
					if (!jsonResult.equals("")) {
						JSONArray jsonArray = jsonResult.getJSONArray("Result");
						for (int i = 0; i < menuItemList.size(); i++) {
							boolean isExist = false;
							for (int j = 0; j < jsonArray.length(); j++) {
								String menuCode2 = jsonArray.getJSONObject(j).getString("FunMenuCode");
								String menuCode1 = menuItemList.get(i).get("ItemCode").toString();
								if (menuCode1.equals(menuCode2)) {
									isExist = true;
									break;
								}
							}
							if (isExist) {
								menuList.add(menuItemList.get(i));
							}
						}
						message.what = Constant.BINDLISTVIEW;
					} else {
						message.obj = "请联系管理员配置功能菜单！";
					}
				} catch (JSONException e) {
					e.printStackTrace();
					message.obj = e.getMessage();
				}
				handler.sendMessage(message);
			}
		});
	}

	private class MenuOnItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			String menuTitle = (String) menuItemList.get(position).get("ItemText");
			String menuCode = (String) menuItemList.get(position).get("ItemCode");
			Bundle bundle = new Bundle();
			bundle.putString("Title", menuTitle);
			bundle.putString("Code", menuCode);
			Message message = new Message();
			message.obj = bundle;
			message.what = Constant.SHOWPAGE;
			handler.sendMessage(message);
		}
	}

	@SuppressLint("HandlerLeak")
	public Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

			switch (msg.what) {
			case Constant.SHOWPAGE:
				Intent intent = new Intent(mContext, ActivityMain.class);
				Bundle bundle = (Bundle) msg.obj;
				String menuTitle = bundle.getString("Title");
				String menuCode = bundle.getString("Code");
				if (menuCode.length() > 10) {
					intent.putExtra("Flag", menuTitle.substring(2));
					dbType = menuTitle.contains("市站") ? "sz" : "jy";
					intent.putExtra("dbType", dbType);
					startActivity(intent);
				} else {
					ShowMsgUtils.ShowToast(mContext, "敬请期待！");
				}
				break;
			case Constant.SHOWTOAST:
				ShowMsgUtils.ShowToast(mContext, (String) msg.obj);
				break;
			case Constant.SHOWMESSAGE:
				ShowMsgUtils.ShowMessage(mContext, (String) msg.obj);
				break;
			case Constant.BINDLISTVIEW:
				menuGridView.setAdapter(menuSimpleAdapter);
				break;
			}
		}
	};

	@Override
	public void onResume() {
		super.onResume();
		ActivityFrame.currFagTag = Constant.STR_FRAGMENT_APPS;
	}
}
